/*
 * Copyright (c) 2020. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre;

import com.andremion.louvre.annotation.StringDef;
import com.andremion.louvre.home.GalleryActivity;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.utils.net.Uri;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
 * A small customizable image picker. Useful to handle an image pick action built-in
 */
public class Louvre {
    /**
     * EXTRA_THEME_COLOR
     */
    public static final String EXTRA_THEME_COLOR = Louvre.class.getPackage().getName() + ".extra.THEME_COLOR";
    /**
     * EXTRA_MAX_SELECTION
     */
    public static final String EXTRA_MAX_SELECTION = Louvre.class.getPackage().getName() + ".extra.MAX_SELECTION";
    /**
     * EXTRA_BUCKET_ID
     */
    public static final String EXTRA_BUCKET_ID = Louvre.class.getPackage().getName() + ".extra.BUCKET_ID";
    /**
     * EXTRA_POSITION
     */
    public static final String EXTRA_POSITION = Louvre.class.getPackage().getName() + ".extra.POSITION";
    /**
     * EXTRA_SELECTION
     */
    public static final String EXTRA_SELECTION = Louvre.class.getPackage().getName() + ".extra.SELECTION";
    /**
     * IMAGES
     */
    public static final String IMAGES = Louvre.class.getPackage().getName() + ".extra.IMAGES";
    /**
     * EXTRA_MEDIA_TYPE_FILTER
     */
    public static final String EXTRA_MEDIA_TYPE_FILTER = Louvre.class.getPackage()
            .getName() + ".extra.MEDIA_TYPE_FILTER";
    /**
     * PREVIEW_REQUEST_CODE
     */
    public static final int PREVIEW_REQUEST_CODE = 11110;
    /**
     * IMAGE_TYPES
     */
    public static final String IMAGE_TYPE_BMP = "image/bmp";
    /**
     * IMAGE_TYPES
     */
    public static final String IMAGE_TYPE_JPEG = "image/jpeg";
    /**
     * IMAGE_TYPES
     */
    public static final String IMAGE_TYPE_PNG = "image/png";
    /**
     * IMAGE_TYPES
     */
    public static final String[] IMAGE_TYPES = {IMAGE_TYPE_BMP, IMAGE_TYPE_JPEG, IMAGE_TYPE_PNG};

    @StringDef({IMAGE_TYPE_BMP, IMAGE_TYPE_JPEG, IMAGE_TYPE_PNG})
    @Retention(RetentionPolicy.SOURCE)
    @interface MediaType {
    }

    private Ability mActivity;
    private AbilitySlice mFragment;
    private int mRequestCode;
    private int mMaxSelection;
    private int mThemeColor = 0;
    private List<Uri> mSelection;
    private String[] mMediaTypeFilter;

    private Louvre(Ability activity) {
        mActivity = activity;
        mRequestCode = -1;
    }

    private Louvre(AbilitySlice fragment) {
        mFragment = fragment;
        mRequestCode = -1;
    }

    /**
     * Louvre
     *
     * @param activity Ability
     * @return Louvre Context
     */
    public static Louvre init(Ability activity) {
        return new Louvre(activity);
    }

    /**
     * Louvre
     *
     * @param fragment Slice
     * @return Louvre Context
     */
    public static Louvre init(AbilitySlice fragment) {
        return new Louvre(fragment);
    }

    /**
     * setRequestCode
     *
     * @param requestCode Code
     * @return Louvre Context
     */
    public Louvre setRequestCode(int requestCode) {
        mRequestCode = requestCode;
        return this;
    }

    /**
     * Set the max images allowed to pick
     *
     * @param maxSelection Value
     * @return Louvre Context
     */
    public Louvre setMaxSelection(int maxSelection) {
        mMaxSelection = maxSelection;
        return this;
    }

    /**
     * Set the max images allowed to pick
     *
     * @param mThemeColor Value
     * @return Louvre Context
     */
    public Louvre setThemeColor(int mThemeColor) {
        this.mThemeColor = mThemeColor;
        return this;
    }

    /**
     * Set the current selected items
     *
     * @param selection Uri List
     * @return Louvre Context
     */
    public Louvre setSelection(List<Uri> selection) {
        mSelection = selection;
        return this;
    }

    /**
     * Set the media type to filter the query with an combination of one of these types:
     * {@link #IMAGE_TYPE_BMP}, {@link #IMAGE_TYPE_JPEG}, {@link #IMAGE_TYPE_PNG}
     *
     * @param mediaTypeFilter Array
     * @return Louvre Context
     */
    public Louvre setMediaTypeFilter(@MediaType String... mediaTypeFilter) {
        mMediaTypeFilter = mediaTypeFilter;
        return this;
    }

    /**
     * open
     */
    public void open() {
        if (mRequestCode == -1) {
            throw new IllegalArgumentException("You need to define a request code in setRequestCode(int) method");
        }
        if (mActivity != null) {
            GalleryActivity.startActivity(mActivity, mRequestCode, mThemeColor, mMaxSelection, mSelection,
                    mMediaTypeFilter);
        } else {
            GalleryActivity.startActivity(mFragment.getAbility(), mRequestCode, mThemeColor, mMaxSelection,
                    mSelection, mMediaTypeFilter);
        }
    }

}
